package domain;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "search_result_item")
public class SearchResultItem extends PersistentEntity implements Comparable<SearchResultItem> {
	
	@JoinColumn(name="userid_fk", referencedColumnName="id")
	@OneToOne
	private User user;
	
	private boolean flagged;
	private String comment;
	
	//hibernate
	public SearchResultItem() {
	}
	
	public SearchResultItem(User user) {
		this.user = user;
	}
	
	public SearchResultItem(User user, boolean flagged) {
		this.user = user;
		this.flagged = flagged;
	}
	
	public User getUser() {
		return user;
	}
	
	public void setUser(User user) {
		this.user = user;
	}
	
	public void setFlagged(boolean flagged) {
		this.flagged = flagged;
	}
	
	public boolean isFlagged() {
		return this.flagged;
	}

	@Override
	public int compareTo(SearchResultItem item) {
		if (item.isFlagged() && this.isFlagged()) return 0;
		if (item.isFlagged()) return 1;
		return -1;
	}
	
	@Override
	public int hashCode() {
        return getUser().hashCode();
    }
	
	@Override
	public boolean equals(Object o) {
		if (o instanceof SearchResultItem) {
			return this.getUser().equals(((SearchResultItem) o).getUser());
		} else if (o instanceof User) {
			return this.getUser().equals((User) o);
		}
		return false;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getComment() {
		return comment;
	}
	
}